package org.ordobill.opensource.telegram.config;

import org.ordobill.opensource.telegram.config.property.ConfigProperty;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * The {@code FileConfig} class represented 
 * 
 * @author Choi Jin Wook(A.K.A Brian Choi @ Ordobill Office) / choijinwook84@gmail.com
 *
 */
public class FileConfig implements Config {
    
    //Configure's attribute class
    private ConfigProperty configProperty;

    /**
     * Contructor FileConfig class.
     * 
     */
    public FileConfig(){}
    
    /**
     * Contructor FileConfig class.
     * 
     * @param fileLocation is {@code String} of configure file location.
     */
    public FileConfig(String fileLocation){
        try {
            this.getProperty(fileLocation);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Contructor FileConfig class.
     * 
     * @param ConfigProperty The {@code ConfigProperty} have configure file location.
     */
    public FileConfig(ConfigProperty configProp){
        try {
            this.getProperty(configProp.getConfigLocation());
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }
    
    /**
     * To load configuration from parameter string's file
     * 
     * @param configLocation
     * @throws Exception
     */
    @SuppressWarnings("resource")
    protected void getProperty(String configLocation) throws Exception {
        
        configProperty = new ConfigProperty(configLocation);

        File readConfig = new File(configLocation);
        
        BufferedReader bufferedReader = new BufferedReader(new FileReader(readConfig));
        
        String lineString = null;
        
        while((lineString = bufferedReader.readLine()) != null){
            
            String[] configString = lineString.split(":");
            
            if(configString.length > 1){
                
                if("splitChar".equals(configString[0]) && configString[0].length() >= 0){
                        if(configString[1] != null){
                            configProperty.setSplitChar(configString[1]);
                        }
                }
                
                if("splitInteger".equals(configString[0]) && configString[0].length() >= 0){
                    
                    String[] splitIntStr = configString[1].split(",");
                    Integer[] splitInt = new Integer[splitIntStr.length];
                    
                    int _idx = 0;
                    
                    for(String intStr: splitIntStr){
                        splitInt[_idx++] = Integer.parseInt(intStr);
                    }
                    
                    configProperty.setSplitInt(splitInt);
                    
                    splitIntStr = null;
                    splitInt    = null;
                    
                }
            }else{
                throw new Exception("설정내용이 올바르지 않습니다.");
            }
        }
    }
    
    /**
     * {@inheritDoc}
     */    
    @Override
    public String getSplitChar() {
        return configProperty.getSplitChar();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer[] getSplitInteger() {
        return configProperty.getSplitInt();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getFileName() {
        return configProperty.getConfigLocation();
    }
    
    
}
